package cn.tang.crm.service;

import java.util.HashMap;
import java.util.Map;

import cn.tang.crm.service.action.ActionService;

public class ActionServiceManager {
	private static final Map<Class<ActionService>, ActionService> asMap = new HashMap<Class<ActionService>, ActionService>();

	@SuppressWarnings("unchecked")
	public static <T> T get(Class<T> clazz) {
		synchronized (asMap) {
			Object rpc = asMap.get(clazz);
			if (rpc == null) {
				try {
					rpc = clazz.newInstance();
					if (rpc instanceof ActionService)
						asMap.put((Class<ActionService>) clazz,
								(ActionService) rpc);
					else
						throw new RuntimeException(
								"only accept class of ActionService");
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
			return (T) rpc;
		}
	}
}
